﻿using System;
using System.Windows.Input;
using Client.Commands;
using Contracts.Entities;
using System.Collections.Generic;
using System.Collections;
using System.Linq;

namespace Client.ViewModels
{
    public abstract class WorkspaceViewModel : BaseViewModel
    {
        private RelayCommand closeCommand;
        private MovieItem[] selectedItems;
        private bool isBusy;

        public WorkspaceViewModel()
        {
            selectedItems = new MovieItem[1];
        }

        #region Properties

        public abstract MovieItem MovieItem { get; set; }

        public virtual bool IsEditMode { get; set; }

        public virtual bool IsListView { get; set; } 
      
        public virtual string DisplayName { get; set; }

        public virtual IEnumerable<MovieItem> MovieItems { get; set; }

        public virtual IEnumerable<MovieItem> SelectedItems
        {
            get
            {
                if (MovieItem != null)
                {
                    selectedItems[0] = MovieItem;
                    return new List<MovieItem>(selectedItems);
                }
                else
                    return new List<MovieItem>();
            }
            set
            {
                selectedItems = (value as IEnumerable<MovieItem>).ToArray();
                NotifyPropertyChanged("SelectedItems");
            }
        }

        public bool IsBusy
        {
            get
            {
                return isBusy;
            }
            set
            {
                isBusy = value;
                NotifyPropertyChanged("IsBusy");
            }
        }

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (closeCommand == null)
                    closeCommand = new RelayCommand(() => this.OnRequestClose(), () => 
                    { 
                        return DisplayName != AllMoviesViewModel.Default.DisplayName; 
                    });

                return closeCommand;
            }
        }

        #endregion

        #region RequestClose [event]

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        private void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
                handler(this, EventArgs.Empty);
        }

        #endregion // RequestClose [event]

    }
}
